<%@ taglib uri="/WEB-INF/vectrics.tld" prefix="vectrics" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles" %>
<%@ taglib uri="/WEB-INF/hexacta.tld"    prefix="hexacta" %>

<%@ page import="org.vectrics.user.Person" %>
<%@ page import="org.vectrics.hibernate.*" %>
<%@ page import="java.util.*" %>
<%@page import="org.vectrics.company.CompanyPerson"%>
<%@page import="org.vectrics.recruiter.RecruitServiceLocator"%>
<%@page import="org.vectrics.employee.Employee"%>

<%
	String userId = request.getParameter("userId");
	if (userId == null) {
		userId = request.getParameter("id");
	}
	if (userId == null) {
		userId = (String)request.getSession(true).getAttribute("userId");
	}
	if ((userId == null) || (userId.trim().equals(""))) {
		userId = request.getParameter("personId");
	}
	
	if ((userId == null) || (userId.trim().length() == 0)) {
		userId = (String)request.getAttribute("userId");
	}
	if ((userId == null) || (userId.trim().length() == 0)) {
		userId = (String)request.getSession(true).getAttribute("userId");
	}
	if ((userId == null) || (userId.trim().length() == 0)) {
		userId = request.getParameter("id");
	}
	if ((userId == null) || (userId.trim().length() == 0)) {
		userId = request.getParameter("personId");
	}
	if ((userId == null) || (userId.trim().length() == 0)) {
		userId = (String)request.getAttribute("personId");
	}
	if ((userId != null) && (userId.equals("current"))) {
		userId = ThreadProperties.getPerson().getId().toString();
	}
	String personId = userId;
	if ((userId == null) || (userId.trim().length() == 0)) {
		throw new RuntimeException("User ID is blank or null");
	}
	
	boolean userCanChangePassword = ThreadProperties.getPerson().hasPrivilege("user.password.edit", ThreadProperties.getDomain());

	if (ThreadProperties.getPerson().getId().equals(new Long(personId))) {
		userCanChangePassword = true;
	}

	Date now = new Date();
	String candidateReportUrl = "/hrs-intranet/report/admin/candidate.pdf?report=candidate&personId="
			+ personId +
			"&time=" + now.getTime();
	
    String companyId = ThreadProperties.getDomain()
                              .getPrimaryCompany().getId().toString();
	
    boolean isEmployee;
    if(request.getAttribute("isEmployee")!=null){
    	isEmployee = (Boolean)request.getAttribute("isEmployee");
    }
    else{
        // Busco el empleado de la compania.
        Employee employee =  RecruitServiceLocator.getCompanyService().findEmployee(new Long(personId), new Long(companyId));
        isEmployee = (employee != null);
    }
	
	
	String skillsEntryLink = request.getContextPath() + "/admin/jobs/candidate/index.do?id=" + userId;
	
	String studiesAddURL = request.getContextPath() + "/admin/user/resume/studiesAdd.do?personId="+ userId;
	
	String experienceAddURL = request.getContextPath() + "/admin/user/resume/experienceAdd.do?personId="+ userId;
	
	String skillEditURL = request.getContextPath() + "/admin/user/resume/skillsEdit.do?personId="+ userId;
	
%>
<table width="100%" >
  <tr>
   <td>

	<div id="editPerson">
		<form action="/hrs-intranet/admin/user/person/edit.do" method="POST">
			<h2><bean:message key="prompt.person.resume.personalData" /></h2>
			<input type="hidden" name="userId" value="<%=userId%>" id="prompt.person.resume.personalData">
			<%if(isEmployee) { %>
				<vectrics:formButton code="button.param" phraseCode="button.person.edit" value="submit" 
					elementId="person.add"
					name="submit"
					permission="manage_employees_items"
					/>
			<%} else { %>
				<vectrics:formButton code="button.param" phraseCode="button.person.edit" value="submit" 
					elementId="person.add"
					name="submit"
					permission="manage_candidate_items"
					/>
			<%}%>
		</form>	
	</div>
	
	<hexacta:userSummaryCV personId="<%=userId%>"/>
	
	<form action="<%=studiesAddURL%>" method="GET">
		<h2><bean:message key="prompt.person.resume.studies" /></h2>
		<input type="hidden" name="personId" value="<%=userId%>">
		<%if(isEmployee) { %>
			<vectrics:formButton code="button.param" phraseCode="button.user.studies.add" value="submit" name="submit"
				permission="manage_employees_items"/>
		<%} else { %>
			<vectrics:formButton code="button.param" phraseCode="button.user.studies.add" value="submit" name="submit"
				permission="manage_candidate_items"/>
		<%} %>
		<hexacta:StudiesView personId="<%=userId%>"/>
	</form>
	
	
	<form action="<%=experienceAddURL%>" method="GET">
		<h2><bean:message key="prompt.person.resume.experience"/></h2>
		<input type="hidden" name="personId" value="<%=userId%>">
		<%if(isEmployee) { %>
			<vectrics:formButton code="button.param" phraseCode="button.experience.add" value="submit" name="submit"
				permission="manage_employees_items"/>
		<%} else { %>
			<vectrics:formButton code="button.param" phraseCode="button.experience.add" value="submit" name="submit"
				permission="manage_candidate_items"/>
		<%}%>
		<hexacta:personExperience personId="<%=userId%>"/>
	</form>
	
	
	<form action="<%=skillEditURL%>" method="GET">
		<h2><bean:message key="prompt.person.resume.skills"/></h2>
		<input type="hidden" name="personId" value="<%=userId%>">
		<%if(isEmployee) { %>
			<vectrics:formButton code="button.param" phraseCode="button.skills.edit" value="submit" name="submit"
				permission="manage_employees_items"/>
		<%} else {%>
			<vectrics:formButton code="button.param" phraseCode="button.skills.edit" value="submit" name="submit"
				permission="manage_candidate_items"/>
		<%}%>
	</form>
	
	<div style="overflow:auto; height:200px;">
		<hexacta:personSkillView personId="<%=userId%>"/>
	</div>
	
	<h2><bean:message key="prompt.person.resume.files" /></h2>

<vectrics:errors />

			<fieldset>
			<legend><bean:message key="prompt.person.resume.upload" /></legend>
			<form action="/hrs-intranet/admin/user/resume/upload.do" method="POST" enctype="multipart/form-data" >
				<input type="hidden" name="userId" value="<%=userId%>">
				<table cellspacing="0" cellspacing="0">
					<tr>
						<td>
							<span class="subFont"><bean:message key="prompt.person.resume.name" />: </span><input type="text" name="name" value="New File" class="formEditClass">&nbsp;
						</td>
						<td>
							<span class="subFont"><bean:message key="prompt.person.resume.file" />: </span><input type="file" name="theFile" class="formEditClass">
						</td>
					</tr>
					<tr>
						<td colspan="2">
							<vectrics:formButton elementId="resume.upload.btn" code="button.param" phraseCode="button.resume.upload" value="submit" name="submit"/> &nbsp;
                            <vectrics:formButton elementId="resume.upload.reset" code="button.param" phraseCode="button.resume.reset" value="cancel" name="cancel"/>
						</td>
					</tr>
				</table>
			</form>
		</fieldset>
<br /> 
<div align="center"> <vectrics:candidateDocuments personId="<%= userId %>" /> </div> 
  </td>
   <td valign="top" align="left" >
		<form id="view_person" action="/hrs-intranet/admin/user/view.do" method="POST">
			<input type="hidden" name="userId" value="<%=userId%>">
			<input type="hidden" name="personId" value="<%=userId%>">
			<vectrics:formButton elementId="view.person.btn" code="button.param" phraseCode="button.candidate.view.person" value="submit" name="submit"/>
		</form>
  </td>
 </tr>
</table>
